<template>
  <c-with-dialog
    ref="dialog"
    v-model="selections"
    multiple
    method="POST"
    :columns="columns"
    url="/api/v1/acceptAreaSeriesDetail/getSeries"
    :filters-raw="filtersRaw"
    title="新增系列"
    :open-fun="openFun"
  >
    <template #btn>
      <c-button @click="cancelHandle">取消</c-button>
      <c-button type="primary" @click="confirmHandle">确认</c-button>
    </template>
  </c-with-dialog>
</template>
<script>
import { otherFiltersItem } from '@/utils'
import typeCode from './typeCode'
export default {
  name: 'AddSeries',
  props: {
    selData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      columns: [
        {
          prop: 'seriesName',
          label: '系列名称'
        },
        {
          prop: 'styleName',
          label: '所属专区'
        },
        {
          prop: 'typeName',
          label: '系列类型'
        },
        {
          prop: 'spaceName',
          label: '所属空间'
        }
      ],
      selections: [],
      otherFilters: [],
      otherRaw: {}
    }
  },
  computed: {
    filtersRaw() {
      const t = this.selData[0]
      const filtersRaw = []
      if (t) {
        filtersRaw.push(otherFiltersItem('spaceCode', t.spaceCode, '='))
        const tc = typeCode[t.areaCode]
        if (tc !== undefined) {
          filtersRaw.push(otherFiltersItem('typeCode', tc, '='))
        }
      }
      return filtersRaw
    }
  },
  watch: {
    selData(v) {
      if (v.length === 0) {
        this.otherFilters = []
        this.otherRaw = {}
        return
      }
      // this.otherFilters = [
      //   otherFiltersItem('areaCode', v[0].areaCode, '=')
      // ]
      if (v[0].spaceCode) {
        this.otherRaw = { spaceCode: v[0].spaceCode }
      }
      this.$nextTick(() => {
        if (this.$refs.cTableBox) {
          this.$refs.cTableBox.initTableData()
        }
      })
    }
  },
  methods: {
    openFun() {
      if (!this.singleJudge(this.selData, '区域', true)) {
        return false
      }
      if (!this.selData[0].configSpacestyleId) {
        this.$message.warning('请选择所属空间')
        return false
      }
      return true
    },
    selectionChange(v) {
      this.selections = v
    },
    confirmHandle() {
      this.$emit('on-change', this.selections)
      this.$refs.dialog.visible = false
    },
    cancelHandle() {
      this.$refs.dialog.visible = false
    }
  }
}
</script>
